/**
 * <copyright>
 *
 * Copyright (c) 2011 Zeligsoft (2009) Limited
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors: 
 *   Zeligsoft (2009) Limited - Initial API and implementation
 *
 * </copyright>
 *
 */
package org.omg.deployment.provider.extended;

import java.util.Collection;
import java.util.Collections;

import org.eclipse.emf.common.command.Command;
import org.eclipse.emf.common.command.CommandWrapper;
import org.eclipse.emf.common.notify.AdapterFactory;
import org.eclipse.emf.common.util.ResourceLocator;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.edit.command.CommandParameter;
import org.eclipse.emf.edit.domain.EditingDomain;
import org.eclipse.emf.edit.provider.IEditingDomainItemProvider;
import org.eclipse.emf.edit.provider.IItemLabelProvider;
import org.eclipse.emf.edit.provider.IStructuredItemContentProvider;
import org.eclipse.emf.edit.provider.ITreeItemContentProvider;
import org.eclipse.emf.edit.provider.ItemProviderAdapter;
import org.omg.deployment.InstanceDeploymentDescription;
import org.omg.xmi.provider.DeploymentplanEditPlugin;

/**
 * @author Toby McClean (toby@zeligsoft.com)
 *
 */
public class TransientInstanceDeploymentDescriptionItemProvider extends
		ItemProviderAdapter implements IEditingDomainItemProvider,
		IStructuredItemContentProvider, ITreeItemContentProvider,
		IItemLabelProvider {

	public TransientInstanceDeploymentDescriptionItemProvider(AdapterFactory adapterFactory,
			InstanceDeploymentDescription instance) {
		super(adapterFactory);
		instance.eAdapters().add(this);
	}
	
	@Override
	public Collection<?> getChildren(Object object) {
		return super.getChildren(target);
	}
	
	@Override
	public Collection<?> getNewChildDescriptors(Object object,
			EditingDomain editingDomain, Object sibling) {
		return super.getNewChildDescriptors(target, editingDomain, sibling);
	}
	
	@Override
	public Object getParent(Object object) {
		return target;
	}
	
	public Object getImage(Object object) {
		return getResourceLocator().getImage("full/obj16/PlanConnectionDescription");
	}
	
	public ResourceLocator getResourceLocator() {
		return DeploymentplanEditPlugin.INSTANCE;
	}
	
	public Command createCommand(final Object object,
			final EditingDomain domain, Class commandClass,
			CommandParameter commandParameter) {
		commandParameter.setOwner(target);
		return super.createCommand(target, domain, commandClass,
				commandParameter);
	}

	protected Command createRemoveCommand(EditingDomain domain, EObject owner,
			EReference feature, Collection collection) {
		return createWrappedCommand(
				super.createRemoveCommand(domain, owner, feature, collection),
				owner);
	}

	protected Command createAddCommand(EditingDomain domain, EObject owner,
			EReference feature, Collection collection, int index) {
		return createWrappedCommand(super.createAddCommand(domain, owner,
				feature, collection, index), owner);
	}

	protected Command createWrappedCommand(Command command, final EObject owner) {
		return new CommandWrapper(command) {
			public Collection getAffectedObjects() {
				Collection affected = super.getAffectedObjects();
				if (affected.contains(owner))
					affected = Collections
							.singleton(TransientInstanceDeploymentDescriptionItemProvider.this);
				return affected;
			}
		};
	}
}
